package CPM;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class dicTrigrams {

	static String[] Stopwords;
	private static int n;
	@SuppressWarnings("null")
	public static String[] getStopwords(){
		FileReader fr = null;
		String[] stop_word_list = null;
		int count=0;
		try {
			fr = new FileReader("//stopwords.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedReader fbr=new BufferedReader(fr);
		try {
			String stopword = fbr.readLine();
			while(stopword!=null){
				stop_word_list[count]=stopword;
				count++;
				stopword=fbr.readLine();

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stop_word_list;
	
	}
	
	public static boolean isStopWord(String[] Stopwords, String str)// exclude stop words from
	// dictionary
	{
		return Arrays.asList(Stopwords).contains(str);

	}

	
	public static void main(String args[]) {
		try {
			Configuration conf=new Configuration();
			String dictionary_name=conf.getDictionaryFile();

			Connection con = null;
			String url = "jdbc:mysql://localhost:3306/";
			String db = "cpmnew";
			String driver = "com.mysql.jdbc.Driver";

			try {
				Class.forName(driver);
				con = DriverManager.getConnection(url + db, "root", "");
			} catch (Exception e) {
				e.printStackTrace();
			}

			HashMap<String, Integer> map = new HashMap<String, Integer>();

			PreparedStatement statement;

			FileReader fr = new FileReader(
					dictionary_name);// reading
			// from
			// dictionary
			BufferedReader fbr = new BufferedReader(fr);

			String str = fbr.readLine();

			StringTokenizer st;
			n = 0;
			String trigram = "";
			String token;

			String finalToken;
			String temp;
			int len;
			int trigramid = 1;
			char[] c;
			String sql;
			String sql1;
			String sql2;
			int number = 0;
			while (str != null) {
				number++;
				sql = "INSERT INTO bioset(number, name) VALUES (?, ?)";
				statement = con.prepareStatement(sql);
				statement.setInt(1, number);
				statement.setString(2, str);
				statement.executeUpdate();

				st = new StringTokenizer(str);// divide string into words
				while (st.hasMoreTokens()) {
					token = st.nextToken();// for each word

					if (isStopWord(Stopwords,token) == false) {// check if it is not
						// stop word

						n++;
						// then only proceed
						temp = token.concat("$$");
						finalToken = "$$".concat(temp);// add $$ before and
						// after word
						len = finalToken.length();

						c = finalToken.toCharArray();// each word to
						// character array
						for (int i = 0; i < (len - 2); i++) {// create trigrams
							int tri = 0;
							char[] cn = { c[i], c[i + 1], c[i + 2] };
							trigram = new String(cn);
							if (map.containsKey(trigram)) {
								tri = map.get(trigram);

							} else {
								tri = trigramid++;
								map.put(trigram, trigramid);
								sql1 = "INSERT INTO trigramset(trigram_id, trigram) VALUES(?, ?)";
								statement = con.prepareStatement(sql1);

								statement.setInt(1, tri);
								statement.setString(2, trigram);
								statement.executeUpdate();
							}

							sql2 = "INSERT INTO idset(number_id, trigram_id) VALUES(?, ?)";
							statement = con.prepareStatement(sql2);

							statement.setInt(1, n);
							statement.setInt(2, tri);
							statement.executeUpdate();

						}

					}
				}

				str = fbr.readLine();
			}

		} catch (Exception e) {

			System.out.println("Error: " + e);
		}

	}
}
